package cn.xuxiu.org.controller;

import cn.xuxiu.bacsic.util.AjaxResult;
import cn.xuxiu.bacsic.util.PageList;
import cn.xuxiu.org.domain.Employee;
import cn.xuxiu.org.query.EmployeeQuery;
import cn.xuxiu.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "部门API", description = "部门的crud操作")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 修改和添加操作
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改操作")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        System.out.println(employee);
        try {
            if (employee.getId() != null){
                employeeService.update(employee);
            } else {
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统故障稍后再试！");
        }
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询")
    public Employee queryById(@PathVariable("id") Long id){
        Employee employee = employeeService.loadById(id);
        return employee;
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public List<Employee> queryAll(){
        List<Employee> employees = employeeService.loadAll();
        return employees;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult removeId(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统故障稍后再试！");
        }
    }

    /**
     * 分页或高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "高级查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        System.out.println(employeeQuery);
        return employeeService.queryPage(employeeQuery);
    }

    @PatchMapping
    @ApiOperation(value = "根据id删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统故障稍后再试！");
        }
    }
}
